/**
 * Created by shudong.msd(风骐) on 2016/4/17.
 */


app.controller('userCenterController', ['$scope', '$http','$state', '$q',function ($scope, $http,$state, $q) {
    var comutil = new commUtils();
    var userInfo = comutil.getUserInfo();
    $scope.user = {"userName": userInfo.name};

    $scope.formData = {};
    $scope.processForm = function (isValid) {
        if (!isValid) {
            $scope.message = '验证失败';
            return;
        }else{
            $scope.message = '';
        }

        $http({
            method: 'POST',
            url: UrlMappings.PWD_CHANGE,
            data: $.param($scope.formData),
            params:{
                'access_token':userInfo.access_token
            },
            headers: {
                'Accept':'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function (data) {

            if (data) {
                alert('修改成功！');
            }else{
                alert('修改失败！');
            }
            angular.element('#changepw_box').modal('hide');
        }).error(function(data){
            $scope.message = "data.detail";
            if(status=='401')comutil.logout($state);
        });
    };

    $scope.lastLoginTime = userInfo.last_login_time;

    $scope.logout = function(){
        //登出
        comutil.logout($state);
    };

    $scope.validateOldPwd = function(pwd){

        var deferred = $q.defer();
        $http.get(UrlMappings.PWD_VALIDATE,{
            params : {
                'password':pwd,
                'access_token':userInfo.access_token
            },
            headers: {
                'Accept':'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function (data) {
            deferred.resolve(data)
        }).error(function(data) {
            deferred.reject(data);
        });
        return deferred.promise;
    }

}]);

